package com.jhws.business.device.service;

import com.jhws.business.device.bo.DevInfo;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.device.bo.DeviceConstant;
import com.jhws.business.device.bo.DeviceFaceRegisterdBo;
import com.jhws.business.device.bo.DeviceUpdateData;
import com.jhws.core.dataUpdate.service.DataUpdateService;
import com.jhws.core.db.BaseDao;
import com.jhws.util.AccountsUtils;
import com.jhws.util.commUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DeviceService {

    @Resource
    private RedisTemplate stringTemplate;
    @Resource
    private ITblBuildingDimService IBuildingService;
    @Resource
    private ITblCommDimService ICommService;
    @Resource
    private BaseDao BaseDao;
    @Resource
    private DataUpdateService dataUpdateService;

    /**
     * 从redis中拿取小区在线设备列表
     *
     * @param commCode
     * @return
     */
    public Map getDeviceListByComm(String commCode) {
        HashOperations<String, String, String> hashOperations = stringTemplate.opsForHash();
        String key = "online:comm:" + commCode;
        Map<String, String> deviceMap = hashOperations.entries(key);
        return deviceMap;
    }

    /**
     * 为指定设备列表填充楼栋别名和单元别名
     *
     * @param comm
     * @param devInfoList
     * @throws Exception
     */
    public List getCommDevices(TblCommDimBo comm, List<DevInfo> devInfoList) throws Exception {
        String commCode = comm.getCommCode();
        for (DevInfo devInfo : devInfoList) {
            String deviceNum = devInfo.getDeviceNum();
            if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.INDOOR_TYPE) {
                String buildingAlias = IBuildingService.getBuildingAliasName(commCode, AccountsUtils.getBuilding(deviceNum), AccountsUtils.getUnit(deviceNum));
                String unitAliasName = IBuildingService.getUnitAliasName(commCode, AccountsUtils.getBuilding(deviceNum), AccountsUtils.getUnit(deviceNum));
                if (StringUtils.isNotEmpty(buildingAlias))
                    devInfo.setBuildingInfo(buildingAlias + " " + unitAliasName);
                else
                    devInfo.setBuildingInfo(AccountsUtils.getUnitInfo(deviceNum));
                devInfo.setDeviceName("室内机-" + deviceNum.substring(deviceNum.length() - 4, deviceNum.length()) + "房");
                devInfo.setType(DeviceConstant.INDOOR_TYPE);
            } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.WALL_TYPE) {
                devInfo.setDeviceName(commUtil.getGateDevAliasName(deviceNum, comm.getGateDevAliasName()));
                devInfo.setType(DeviceConstant.WALL_TYPE);
            } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.MANAGE_TYPE) {
                devInfo.setDeviceName("管理机-" + deviceNum.substring(deviceNum.length() - 1, deviceNum.length()));
                devInfo.setType(DeviceConstant.MANAGE_TYPE);
            } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.VOICE_GATEWAY_TYPE) {
                devInfo.setDeviceName("语音网关");
                devInfo.setType(DeviceConstant.VOICE_GATEWAY_TYPE);
            } else {
                String buildingAlias = IBuildingService.getBuildingAliasName(commCode, AccountsUtils.getBuilding(deviceNum), AccountsUtils.getUnit(deviceNum));
                String unitAliasName = IBuildingService.getUnitAliasName(commCode, AccountsUtils.getBuilding(deviceNum), AccountsUtils.getUnit(deviceNum));
                if (StringUtils.isNotEmpty(buildingAlias))
                    devInfo.setBuildingInfo(buildingAlias + " " + unitAliasName);
                else
                    devInfo.setBuildingInfo(AccountsUtils.getUnitInfo(deviceNum));
                devInfo.setDeviceName("门口机-" + deviceNum.substring(deviceNum.length() - 1, deviceNum.length()));
                devInfo.setType(DeviceConstant.OUTDOOR_TYPE);
            }
        }
        return devInfoList;
    }

    /**
     * 开通或关闭某设备的人脸权限
     *
     * @param state     0表示关闭，1表示开通
     * @param devNumber 设备编号
     * @return 成功返回null, 失败返回错误信息
     */
    public String updateFacePerms(Integer state, String devNumber, String push) throws Exception {

        if (AccountsUtils.devicesType(devNumber) != DeviceConstant.OUTDOOR_TYPE && AccountsUtils.devicesType(devNumber) != DeviceConstant.WALL_TYPE)
            return "设备类型不对";

        Long timestamp = System.currentTimeMillis();
        String commCode = AccountsUtils.getCommCode(devNumber);
        if (state == 0) {
            BaseDao.executeHql("delete from DeviceFaceRegisterdBo where comm_code = '" + commCode + "' and dev_number = '" + devNumber + "'");
        } else {
            TblCommDimBo comm = ICommService.findTblCommDimById(commCode);
            Long exits = BaseDao.getCountByHql("select count(*) from DeviceFaceRegisterdBo where comm_code = '" + commCode + "'");
            DeviceFaceRegisterdBo deviceFaceRegisterd = BaseDao.get(DeviceFaceRegisterdBo.class, devNumber);
            if (deviceFaceRegisterd == null) {
                if (exits >= comm.getFaceDevCount())
                    return "开通失败,该小区人脸Lisence数量不够";
                else {
                    DeviceFaceRegisterdBo deviceFaceRegisterdBo = new DeviceFaceRegisterdBo();
                    deviceFaceRegisterdBo.setCommCode(commCode);
                    deviceFaceRegisterdBo.setDevNumber(devNumber);
                    deviceFaceRegisterdBo.setTimestamp(timestamp);
                    BaseDao.saveOrUpdate(deviceFaceRegisterdBo);
                }
            }
        }

        if (push != null)
            dataUpdateService.pushAndUpdate(timestamp, DeviceUpdateData.DEV_FACE_PERM, devNumber, true, false, null, null);
        return null;
    }

    /**
     * 根据条件查找人脸注册权限列表
     *
     * @param where
     * @return
     */
    public List<DeviceFaceRegisterdBo> getDevFaceRegisteredByWhere(String where) {
        return BaseDao.findList("from DeviceFaceRegisterdBo " + where);
    }

    public DeviceFaceRegisterdBo getDevFaceRegistered(String devNumber) {
        return BaseDao.get(DeviceFaceRegisterdBo.class, devNumber);
    }
}
